package com.qc.dao.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.qc.common.StringUtil;
import com.qc.dao.SecretaryDao;
import com.qc.entity.SecretaryEntity;

@Repository("secretaryDao")
public class SecretaryDaoImpl implements SecretaryDao{

	@Resource(name = "jdbcTemplate")
    private JdbcTemplate jdbcTemplate;
	
	@Override
	public int getListSecretaryCount(String name) {

		String sql = "select count(*) from t_secretary where 1 = 1 and if_del = '0' and level = '1' ";
        if (StringUtil.isNotEmpty(name) && !"undefined".equals(name) ) {
            sql += " and name like '%"+name+"%' ";
        }
        return jdbcTemplate.queryForInt(sql);
	}

	@Override
	public List<Map<String, Object>> getAllSecretary(String name, int pageNo,int pageSize) {

		int beginRow = (pageNo - 1)* pageSize;
        String sql = "select id,name,price from t_secretary where 1 = 1 and if_del = '0' and level = '1' ";
        
        if (StringUtil.isNotEmpty(name) && !"undefined".equals(name) ) {
            sql += " and name like '%"+name+"%' ";
        }
        
        sql += " order by created_time DESC ";
        sql += " LIMIT "+ String.valueOf(beginRow)+ "," + pageSize;
        
        return jdbcTemplate.queryForList(sql);
	}

	@Override
	public int saveSecretary(SecretaryEntity secretaryEntity) {

		String sql = "insert into t_secretary(name,parent,price,level) values(?,?,?,?)";
        return jdbcTemplate.update(sql,new Object[]{secretaryEntity.getName(),secretaryEntity.getParent(),secretaryEntity.getPrice(),secretaryEntity.getLevel()});
	}

	@Override
	public int deleteSecretaryId(long id) {

		String sql = "update t_secretary set if_del = '1' where id = " +id;
        return jdbcTemplate.update(sql);
	}

	@Override
	public int updateSecretaryId(SecretaryEntity secretaryEntity) {

		String sql = "update t_secretary set name = ? , price = ? where id = ? ";
        return jdbcTemplate.update(sql,new Object[]{secretaryEntity.getName(),secretaryEntity.getPrice(),secretaryEntity.getId()});
	}

	@Override
	public List<Map<String, Object>> getAllLevleOne() {
		
		String sql = "select id,name,price from t_secretary where 1 = 1 and if_del = '0' and level = '1' ";
		return jdbcTemplate.queryForList(sql);
	}

	@Override
	public int getListSecretaryCountTwo(String name,String parent) {
		
		String sql = "select count(*) from t_secretary where 1 = 1 and if_del = '0' and level = '2' ";
		
        if (StringUtil.isNotEmpty(name) && !"undefined".equals(name) ) {
            sql += " and name like '%"+name+"%' ";
        }
        
        if (StringUtil.isNotEmpty(parent) && !"undefined".equals(parent) ) {
            sql += " and parent ='"+parent+"' ";
        }
        return jdbcTemplate.queryForInt(sql);
	}

	@Override
	public List<Map<String, Object>> getAllSecretaryTwo(String name,String parent,int pageNo, int pageSize) {

		int beginRow = (pageNo - 1)* pageSize;
        String sql = "select id,name,price from t_secretary where 1 = 1 and if_del = '0' and level = '2' ";
        
        if (StringUtil.isNotEmpty(name) && !"undefined".equals(name) ) {
            sql += " and name like '%"+name+"%' ";
        }
        
        if (StringUtil.isNotEmpty(parent) && !"undefined".equals(parent) ) {
            sql += " and parent ='"+parent+"' ";
        }
        
        sql += " order by created_time DESC ";
        sql += " LIMIT "+ String.valueOf(beginRow)+ "," + pageSize;
        
        return jdbcTemplate.queryForList(sql);
	}

}
